import 'package:flutter/material.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';
import 'package:xindong/routes/call/controller/xindong_call_view_ctr.dart';
import 'package:xindong/routes/call/view/xindong_call_control_view.dart';
import 'package:xindong/routes/call/view/xindong_call_rtc_audio_view.dart';
import 'package:xindong/routes/call/view/xindong_call_rtc_video_view.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/xindong_common_burn_layer.dart';

// 1v1 见面
class XinDongCallYeMian extends StatefulWidget {
  const XinDongCallYeMian({Key? key}) : super(key: key);

  @override
  State<XinDongCallYeMian> createState() => _XinDongCallYeMianState();
}

class _XinDongCallYeMianState extends State<XinDongCallYeMian> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      // 拉取用户信息
      // if (XinDongCallProvider.instance.targetUser.isFavorite == null) {
      //   XinDongUserClass? result = await XinDongUserHandle.requestBaseUser(XinDongCallProvider.instance.targetUser.uid);
      //   if (result != null) {
      //     XinDongCallProvider.instance.targetUser = result;
      //     XinDongCallProvider.instance.notifyListeners();
      //   }
      // }
    });
  }

  @override
  Widget build(BuildContext context) {
    bool isAudioMode = XinDongCallController.to.currentCallType == XinDongCallType.audioCall;
    bool isVideoMode = XinDongCallController.to.currentCallType == XinDongCallType.videoCall;
    return WillPopScope(
      onWillPop: () async => false,
      child: Scaffold(
        resizeToAvoidBottomInset: false, //输入框抵住键盘 内容不随键盘滚动
        backgroundColor: Colors.grey.shade600,
        body: GestureDetector(
          onTap: () {
            // 触摸收起键盘
            FocusScope.of(context).requestFocus(FocusNode());

            // 改变控制栏显示隐藏
            XinDongCallViewController.to.changeViewControlOpacity();
          },
          behavior: HitTestBehavior.translucent,
          child: Stack(
            fit: StackFit.expand,
            children: [
              // 只为状态栏设置
              XinDongAppBar(
                "",
                brightness: Brightness.dark,
                toolbarHeight: 0,
                automaticallyImplyLeading: false,
                backgroundColor: Colors.transparent,
                elevation: 0,
              ),

              // 语音背景图
              if (isAudioMode)
                ImageUtil.networkImageUrl(
                  XinDongCallController.to.targetUser.getAvatar,
                  placeholder: const SizedBox(),
                ),

              // 背景主题图
              if (isVideoMode)
                ImageUtil.avatarUrl(XinDongCallController.to.mineUser.getAvatar, placeholder: const SizedBox()),

              // 毛玻璃层
              const XinDongCommonBurnLayer(opacity: 0.8, color: Colors.white38),

              // 视频层/语音层
              isVideoMode
                  ? const XinDongCallRTCVideoPreview()
                  : isAudioMode
                      ? const XinDongCallRTCAudioView()
                      : const SizedBox(),

              // 房间控制层
              const XinDongCallControlView(),
            ],
          ),
        ),
      ),
    );
  }
}
